import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpResponse, HttpRequest,  HttpParams } from '@angular/common/http';
import { Observable, throwError } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { Router } from '@angular/router';
@Injectable({
  providedIn: 'root'
})
export class RequestService {
  private baseUrl = 'http://localhost:8080/api/';
  headers = new HttpHeaders();
  getHeaders():HttpHeaders {
      let headers = new HttpHeaders();
      
      headers.append('Access-Control-Allow-Origin' , '*');
      headers.append('Access-Control-Allow-Methods', 'POST, GET, OPTIONS, PUT');
      headers.append('Accept','application/json');
      headers.append('content-type','application/json');
      return headers;
  }

  constructor(
      private http: HttpClient,
      private router: Router,
  ) {}

  get(url:string, urlParmas?:any):Observable<any> {
    let me = this;
    return this.http.get(this.baseUrl + url, {headers:this.getHeaders(),  responseType:"json", params:urlParmas} )
        .pipe(
            catchError(error => {
                console.log("Some error in catch");
                
                if (error.status === 401 || error.status === 403){
                    me.router.navigate(['/login']);
                }
                return throwError(error || 'Server error');
            })
        );
  }

  post(url:string, body:any):Observable<any> {
    let me = this;
        return this.http.post(this.baseUrl + url, body, { headers:this.getHeaders()})
            .pipe(
                catchError(error => {
                    console.log("Some error in catch");
                    console.log("status:",error.status);
                    if (error.status === 401 || error.status === 403){
                        me.router.navigate(['/login']);
                    }
                    return throwError(error || 'Server error');
                })
            );
  }
  put(url:string, body:any):Observable<any>{
    let me = this;
    return this.http.put(this.baseUrl + url, body, { headers:this.getHeaders()})
        .pipe(
            catchError(error => {
                console.log("Some error in catch");
                if (error.status === 401 || error.status === 403){
                    me.router.navigate(['/login']);
                }
                return throwError(error || 'Server error');
            })
        );
}

delete(url:string):Observable<any>{
    console.log('调用http delete')
    let me = this;
    return this.http.delete(this.baseUrl + url, { headers:this.getHeaders()},)
        .pipe(
            catchError(error => {
                console.log("Some error in catch");
                if (error.status === 401 || error.status === 403){
                    me.router.navigate(['/login']);
                }
                return throwError(error || 'Server error');
            })
        );
}

deleteTest(url:string):void{
    let baseUrl1 = 'http://localhost:8080/api/customers';
    console.log('1')
   this.http.delete(`${baseUrl1}` + `/delete`, { responseType: 'text' });
    console.log('2')
}
}
